﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;


namespace Common
{
    public abstract class LocalCacheFactory {
        internal static List<T> ListFromFile<T>(string path) {
            XmlSerializerFactory factory = new XmlSerializerFactory();
            XmlSerializer serializer = factory.CreateSerializer(typeof(List<T>), new Type[] { typeof(T) });
                        
            using(FileStream fs = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,path))) {
                object cacheData = serializer.Deserialize(fs);
                return cacheData == null ? new List<T>() : (List<T>)cacheData;
            }
        }

        internal static void ListToFile<T>(string path, List<T> obj) {
            XmlSerializerFactory factory = new XmlSerializerFactory();
            XmlSerializer serializer = factory.CreateSerializer(typeof(List<T>), new Type[] { typeof(T) });

            using(FileStream fs = File.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path), FileMode.Create)) {
                serializer.Serialize(fs, obj);
                fs.Flush();
            }
        }

        public static T FromFile<T>(string path) {
            if(!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path))) {
                return default(T);
            }

            try {
                XmlSerializerFactory factory = new XmlSerializerFactory();
                XmlSerializer serializer = factory.CreateSerializer(typeof(T));

                using(FileStream fs = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path))) {
                    object cacheData = serializer.Deserialize(fs);
                    return cacheData == null ? default(T) : (T)cacheData;
                }
            }
            catch(Exception e) {
                StringBuilder error = new StringBuilder();
                error.AppendLine(e.Message);
                error.AppendLine();
                error.AppendFormat("{0}", path);
                error.AppendLine();
                error.AppendFormat("{0}", AppDomain.CurrentDomain.BaseDirectory);
                //throw new Exception(error.ToString());
            }
            return default(T);
        }

        public static void ToFile<T>(string path, T obj)
        {
            if(!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path))) {
                File.Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)).Close();
            }

            XmlSerializerFactory factory = new XmlSerializerFactory();
            XmlSerializer serializer = factory.CreateSerializer(typeof(T));

            try {
                using(FileStream fs = File.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path), FileMode.Create)) {
                    serializer.Serialize(fs, obj);
                    fs.Flush();
                }
            }
            catch(Exception e) {
                StringBuilder error = new StringBuilder();
                error.AppendLine(e.Message);
                error.AppendLine();
                error.AppendFormat("{0}", path);
                error.AppendLine();
                error.AppendFormat("{0}", AppDomain.CurrentDomain.BaseDirectory);

                throw new Exception(error.ToString());
            }
        }
    }
}